package com.stone.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.stone.model.Admin;
import com.stone.model.Recommend;
import com.stone.model.Stone;
import com.stone.service.ILoggerService;
import com.stone.service.IRecommendService;
import com.stone.service.IStoneService;
import com.stone.serviceimpl.LoggerService;
import com.stone.utils.LogConstrants;

public class RecommendAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8303010546299965314L;
	
	private IRecommendService recommendService;
	private IStoneService stoneService;
	private ILoggerService logService = new LoggerService();
	
	private String selectedStones;
	private List<Stone> stonesList;
	private List<Stone> aleadyRemommendedList;
	
	private static Logger logger = Logger.getLogger("石材推荐");
	//-------------set and get
	
	public String getSelectedStones() {
		return selectedStones;
	}
	public IRecommendService getRecommendService() {
		return recommendService;
	}
	public void setRecommendService(IRecommendService recommendService) {
		this.recommendService = recommendService;
	}
	public IStoneService getStoneService() {
		return stoneService;
	}
	public void setStoneService(IStoneService stoneService) {
		this.stoneService = stoneService;
	}
	public void setSelectedStones(String selectedStones) {
		this.selectedStones = selectedStones;
	}
	
	public List<Stone> getStonesList() {
		return stonesList;
	}
	public void setStonesList(List<Stone> stonesList) {
		this.stonesList = stonesList;
	}
	
	public List<Stone> getAleadyRemommendedList() {
		return aleadyRemommendedList;
	}
	public void setAleadyRemommendedList(List<Stone> aleadyRemommendedList) {
		this.aleadyRemommendedList = aleadyRemommendedList;
	}
	
	// ---------------actions
	/**
	 * 配置推荐石材
	 * 	- 先将表里原有的数据清空
	 *  - 再根据所选择的 ID 列表 新建新的Recommend
	 *  - 再将这些Recommend 保存到数据库
	 */
	public String recommend(){
		List<Recommend> list = recommendService.findAllRecommend();
		for(Recommend r : list){
			recommendService.deleteRecommend(r);
		}
		
		String [] ids = selectedStones.split(";");
		Date date = new Date();
		for(String str : ids){
			Recommend recommend = new Recommend();
			recommend.setStone(stoneService.findStoneById(Long.parseLong(str)));
			recommend.setTime(date);
			recommendService.addRecommend(recommend);
		}
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		logger.fatal(((Admin)session.get("admin")).getUsername()+"更新了推荐石材 ");
		logService.log(((Admin)session.get("admin")).getUsername(), "更新了推荐石材 ", LogConstrants.RECOMMEND_UPDATE);
		
		return SUCCESS;
	}
	
	public String listStones(){
		stonesList = stoneService.findStonePageByName("", 1, 50).getList() ; // 用于显示的供应商列表
		aleadyRemommendedList = new ArrayList<Stone>();
		List<Recommend> list = recommendService.findAllRecommend();
		for(Recommend r : list){
			stonesList.remove(stoneService.findStoneById(r.getStone().getId()));
			aleadyRemommendedList.add(stoneService.findStoneById(r.getStone().getId()));
		}
		
		return SUCCESS;
	}
	
}
